/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.grnt.buddy;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.grnt.dbcommon.IdExtractor;
import org.grnt.dbcommon.DataControler.DataControlException;
import org.grnt.dbcommon.DataControler;

/**
 *
 * This is a servlet for demonstrating how to implement add user semantic.
 * @author sayid
 */
public class AddUserSlt extends HttpServlet{
	Long   userId    = null;
	String loginName = null;
	String password  = null;
	String msg       = null;
	Long   rootId    = null;

	private final void output(HttpServletResponse response) throws IOException {
		response.setContentType("text/html");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");
		ServletOutputStream out = response.getOutputStream();
		out.println("<head><title>Just a basic servlet</title></head>");
		out.println("<body>");
		out.println("user name is :" + loginName);
		out.println("<br/>");
		out.println("password is :" + password);
		out.println("<br/>");
		out.println("user id is :" + userId);
		out.println("<br/>");
		out.println("root id is :" + rootId);
		out.println("<br/>");
		out.println("<h1>" + msg + "</h1>");
		out.println("</body>");
		out.close();
	}

	/**
	 *
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 *
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		ServletOutputStream out = response.getOutputStream();

		loginName = request.getParameter("userName");
		password = request.getParameter("password");

		if ((loginName == null) || ("".equals(loginName.trim()))) {
			msg = "login name is empty.";
			output(response);
			return;
		}

		if ((password == null) || ("".equals(password.trim()))) {
			msg = "password is empty.";
			output(response);
			return;
		}

		loginName.trim();
		password.trim();

		try {
			userId = IdExtractor.nextId("user_id");
			rootId = IdExtractor.nextId("buddy_id");
			AddUser addUser = new AddUser(userId, loginName, password, rootId);
			DataControler.update(addUser);
			msg = "successful!";
		} catch (DataControlException ex) {
			Logger.getLogger(AddUserSlt.class.getName()).log(Level.SEVERE, null, ex);
			msg = ex.getMessage();
			ex.printStackTrace();
		} finally {
			output(response);
		}
	}

}
